﻿using System;
using System.Data.Common;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Assist;

namespace HYFamilyLibDataManager
{
	public class SqliteDatabase : FamilyLibDatabase, IDisposable
	{
		public static string SqlitePath
		{
			get
			{
				return Path.Combine(AssistFunc.GetAppRootPath(), "bin\\" + SqliteDatabase.SqliteName);
			}
		}

		public override string ConnStr
		{
			get
			{
				return "Data Source=" + this.mName + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10";
			}
		}

		public SqliteDatabase(string fileName)
		{
			this.mName = fileName;
			this.Sqlite = Assembly.LoadFrom(SqliteDatabase.SqlitePath);
			object obj = this.Sqlite.CreateInstance(SqliteDatabase.SQLiteConnection, true, BindingFlags.CreateInstance, null, new object[]
			{
				this.ConnStr
			}, null, new object[0]);
			obj.GetType().GetMethod("Open").Invoke(obj, new object[0]);
			this.Connection = (obj as DbConnection);
		}

		public override ISubjectTable SubjectTable
		{
			get
			{
				return new SqliteSubjectTable(this);
			}
		}

		public override ICategoryTable CategoryTable
		{
			get
			{
				return new SqliteCategoryTable(this);
			}
		}

		public override ICategoryParamTable CategoryParamTable
		{
			get
			{
				return new SqliteCategoryParamTable(this);
			}
		}

		public override IFamilyTable FamilyTable
		{
			get
			{
				return new SqliteFamilyTable(this);
			}
		}

		public override IFamilyParamTable FamilyParamTable
		{
			get
			{
				return new SqliteFamilyParamTable(this);
			}
		}

		public override ITypeTable TypeTable
		{
			get
			{
				return new SqliteTypeTable(this);
			}
		}

		public override IFamilyFileTable FamilyFileTable
		{
			get
			{
				return new SqliteFamilyFileTable(this);
			}
		}

		public override IFamilyImgTable FamilyImgTable
		{
			get
			{
				return new SqliteFamilyImgTable(this);
			}
		}

		public SqliteDatabase CopyForUpdate()
		{
			FileInfo fileInfo = new FileInfo(this.mName);
			int num = this.mName.LastIndexOf('.');
			string str = this.mName.Substring(0, num - 4);
			int num2 = Convert.ToInt32(this.mName.Substring(num - 4, 4)) + 1;
			string text = str + num2.ToString() + ".db";
			try
			{
				string fullName = fileInfo.CopyTo(text).FullName;
			}
			catch
			{
				if (MessageBox.Show(string.Format("升级的目标{0}库已经存在，是否跳过？", this.ToString()), "跳过", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
				{
					return null;
				}
			}
			SqliteDatabase sqliteDatabase = new SqliteDatabase(new FileInfo(text).FullName);
			sqliteDatabase.SubjectTable.SetVersion(num2.ToString());
			return sqliteDatabase;
		}

		public void Dispose()
		{
			this.Connection.Close();
			this.Connection.Dispose();
		}

		public static string SqliteName = "System.Data.SQLite.dll";

		public static string SQLiteConnection = "System.Data.SQLite.SQLiteConnection";

		public static string SQLiteParameter = "System.Data.SQLite.SQLiteParameter";

		public Assembly Sqlite;
	}
}
